package com.ryan.ohos.extension.nested;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentParent;

import static com.ryan.ohos.extension.nested.NestedScrollingHelper.SCROLL_AXIS_NONE;
import static com.ryan.ohos.extension.nested.NestedScrollingHelper.TYPE_NON_TOUCH;

/**
 * Helper class for implementing nested scrolling parent views
 *
 * <p>{@link ohos.agp.components.ComponentContainer ComponentContainer} subclasses should instantiate a final instance
 * of this class as a field at construction. For each <code>ViewGroup</code> method that has
 * a matching method signature in this class, delegate the operation to the helper instance
 * in an overridden method implementation. This implements the standard framework policy
 * for nested scrolling.</p>
 */
public class NestedScrollingParentHelper {

    private int mNestedScrollAxesTouch;
    private int mNestedScrollAxesNonTouch;

    public NestedScrollingParentHelper(ComponentParent viewGroup) {
    }

    /**
     * Called when a nested scrolling operation initiated by a descendant view is accepted
     * by this ViewGroup.
     *
     * <p>This is a delegate method. Call it from your {@link ohos.agp.components.ComponentContainer ComponentContainer}
     * subclass method/{@link NestedScrollingParent} interface method with
     * the same signature to implement the standard policy.</p>
     */
    public void onNestedScrollAccepted(Component child, Component target, int axes, int type) {
        if (type == TYPE_NON_TOUCH) {
            mNestedScrollAxesNonTouch = axes;
        } else {
            mNestedScrollAxesTouch = axes;
        }
    }

    /**
     * React to a nested scroll operation ending.
     *
     * <p>This is a delegate method. Call it from your {@link ohos.agp.components.ComponentContainer ComponentContainer}
     * subclass method/{@link NestedScrollingParent} interface method with
     * the same signature to implement the standard policy.</p>
     */
    public void onStopNestedScroll(Component target, int type) {
        if (type == TYPE_NON_TOUCH) {
            mNestedScrollAxesNonTouch = SCROLL_AXIS_NONE;
        } else {
            mNestedScrollAxesTouch = SCROLL_AXIS_NONE;
        }
    }

    /**
     * Return the current axes of nested scrolling for this ViewGroup.
     *
     * <p>This is a delegate method. Call it from your {@link ohos.agp.components.ComponentContainer ComponentContainer}
     * subclass method/{@link NestedScrollingParent} interface method with
     * the same signature to implement the standard policy.</p>
     */
    public int getNestedScrollAxes() {
        return mNestedScrollAxesTouch | mNestedScrollAxesNonTouch;
    }
}
